/**
 *  The Rooms - Tentative Roguelike by Elvio "DasK" Sarquis
 *  Copyright (C) 2010  Elvio Sarquis
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/.
 */

package rooms;

import net.slashie.libjcsi.wswing.WSwingConsoleInterface;
import net.slashie.libjcsi.ConsoleSystemInterface;
/**
 * 
 * @author Elvio "DasK" Sarquis
 */

public class Main {
//Parte estatica
private static Main main;
public static void main(String args[]){
        main = new Main();
}
//Parte instanciada

    private ConsoleSystemInterface display;
    private Render render;
    private Engine con;
    private int state;
    private int inmenu;
    private int menuopt;
    private boolean running;

    public Main(){

        display = new WSwingConsoleInterface("The ROOMS", true);
        render = new Render();
        con = new Engine();
        state = 0;
        menuopt = 0;
        running = true;

        bucle();
    }

    private void bucle(){

        while(running){

            if(render.getState() != state) render.setState(state);

            switch(state){

                case C.sMENU:

                    render.setOption(menuopt);
                    render.render();
                    show(render.getBuffer());
                    input();

                    break;

                case C.sGAME:

                    if(con.getInMenu() != inmenu){
                        inmenu = con.getInMenu();
                        render.setInMenu(inmenu);
                    }

                    switch(inmenu){

                        case C.imNORMAL:

                            render.setMap(con.getMapa());
                            render.setChars(con.getChars());
                            render.setMsgs(con.getBuffer());
                            render.render();
                            show(render.getBuffer());
                            input();
                            con.turnAI();
                            con.endTurn();

                            break;

                        case C.imDEAD:

                            render.setMap(con.getMapa());
                            render.setChars(con.getChars());
                            render.setMsgs(con.getBuffer());
                            render.render();
                            show(render.getBuffer());
                            display.inkey();
                            end();
                            break;

                        case C.imINVENT:

                            break;
                    }
                    break;
            }
        }
    }

    private void show(Screen screen){

        display.cls();

        for(int j=0; j<screen.getY(); j++)
        for(int i=0; i<screen.getX(); i++){
            display.print(i, j, screen.getChar(i, j), screen.getColor(i, j));
        }
        
        display.refresh();
        
    }

    private void input(){

        boolean acepted = false;

        while(!acepted){

            int key = display.inkeyint();
            switch(state){
                case C.sMENU:
                    acepted = inputMenu(key); break;

                case C.sGAME:
                    acepted = inputGame(key); break;
            }
        }
    }

    private boolean inputMenu(int key){

           if(key == Keys.ARROWUP){
               if(menuopt>0)
                    menuopt--;
           }else if(key == Keys.ARROWDOWN){
               if(menuopt<3)
                    menuopt++;
           }else if(key == Keys.ENTER){
                switch(menuopt){
                    case 0:
                        state = 1; break;

                    case 1:
                        con.load();
                        state = 1;
                        break;

                    case 2:
                        state=C.sOPTIONS; break;

                    case 3:
                        end(); break;
                }

           }else if(key == Keys.ESC){
               end();
           }else{
               System.out.println(key);
                return false;
           }
        return true;
    }
    
    private boolean inputGame(int key){

    if(Keys.isGameKey(key)){

            return con.command(key);
        }
        return false;
    }
    
    private void end(){
        System.exit(0);
    }
}